// 百度地图API功能
var map = new BMap.Map("map");
var printjson = {
  panels: [
    {
      index: 0,
      paperType: "A4",
      height: 297,
      width: 210,
      paperHeader: 0,
      paperFooter: 805,
      printElements: [
        {
          tid: "configModule.name",
          options: {
            left: 20,
            top: 25,
            height: 42,
            width: 107,
            fontSize: 19,
            fontWeight: "700",
            textAlign: "center",
            lineHeight: 39,
            hideTitle: true,
          },
        },
        {
          tid: "configModule.email",
          options: { left: 390, top: 35, height: 13, width: 165 },
        },
        {
          tid: "configModule.address",
          options: { left: 390, top: 55, height: 13, width: 165 },
        },
        {
          tid: "configModule.phone",
          options: { left: 390, top: 75, height: 13, width: 165 },
        },
        {
          tid: "configModule.hline",
          options: { left: 10, top: 110, height: 10, width: 573 },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 20,
            top: 135,
            height: 13,
            width: 27,
            title: "目标",
            textAlign: "center",
          },
        },
        {
          tid: "configModule.target",
          options: {
            left: 20,
            top: 160,
            height: 13,
            width: 562,
            fontSize: 13,
            hideTitle: "1",
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 20,
            top: 195,
            height: 13,
            width: 29,
            title: "教育",
            textAlign: "center",
          },
        },
        {
          tid: "configModule.professional",
          options: {
            left: 20,
            top: 230,
            height: 17,
            width: 561,
            fontSize: 12,
            fontWeight: "600",
            lineHeight: 17,
            hideTitle: "1",
          },
        },
        {
          tid: "configModule.university",
          options: {
            left: 20,
            top: 255,
            height: 15,
            width: 561,
            fontWeight: "600",
            hideTitle: "1",
          },
        },
        {
          tid: "configModule.universityAddress",
          options: {
            left: 20,
            top: 280,
            height: 13,
            width: 120,
            hideTitle: "1",
          },
        },
        {
          tid: "configModule.universityDate",
          options: { left: 20, top: 305, height: 13, width: 120 },
        },
        {
          tid: "configModule.tech",
          options: { left: 20, top: 345, height: 40, width: 561 },
        },
        {
          tid: "configModule.workExperience",
          options: {
            left: 20,
            top: 420,
            height: 44,
            width: 559,
            columns: [
              [
                {
                  width: 139.75,
                  title: "职位",
                  columnId: "position",
                  fixed: false,
                  rowspan: 1,
                  colspan: 1,
                  align: "center",
                },
                {
                  width: 139.75,
                  title: "公司",
                  columnId: "company",
                  fixed: false,
                  rowspan: 1,
                  colspan: 1,
                  align: "center",
                },
                {
                  width: 139.75,
                  title: "地点",
                  columnId: "address",
                  fixed: false,
                  rowspan: 1,
                  colspan: 1,
                  align: "center",
                },
                {
                  width: 139.75,
                  title: "时间",
                  columnId: "date",
                  fixed: false,
                  rowspan: 1,
                  colspan: 1,
                  align: "center",
                },
              ],
            ],
          },
        },
      ],
      paperNumberLeft: 565,
      paperNumberTop: 819,
    },
    {
      index: 1,
      height: 148,
      width: 210,
      paperHeader: 0,
      paperFooter: 419.52755905511816,
      printElements: [
        {
          tid: "configModule.customText",
          options: {
            left: 208.5,
            top: 31.5,
            height: 9.75,
            width: 163.5,
            title: "大生新能源",
            fontFamily: "SimSun",
            fontSize: 20.25,
            letterSpacing: 12,
            textAlign: "center",
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 253.5,
            top: 54,
            height: 9.75,
            width: 120,
            title: "入库单",
            fontSize: 17.25,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 373.5,
            top: 85.5,
            height: 9.75,
            width: 64.5,
            title: "入库单号：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 447,
            top: 85.5,
            height: 9.75,
            width: 120,
            title: "编码",
            field: "PackNo",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 375,
            top: 108,
            height: 9.75,
            width: 63,
            title: "日期：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.universityDate",
          options: {
            left: 447,
            top: 109.5,
            height: 9.75,
            width: 120,
            dataType: "datetime",
            format: "yyyy-MM-dd",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 375,
            top: 132,
            height: 9.75,
            width: 61.5,
            title: "入库货仓：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 447,
            top: 132,
            height: 9.75,
            width: 120,
            title: "成品仓",
            field: "InLib",
            testData: "成品仓",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.hline",
          options: { left: 16.5, top: 156, height: 9, width: 559.5 },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 378,
            top: 172.5,
            height: 15,
            width: 30,
            title: "数量",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 442.5,
            top: 172.5,
            height: 15,
            width: 30,
            title: "单位",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 93,
            top: 172.5,
            height: 15,
            width: 43.5,
            title: "编码",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 202.5,
            top: 174,
            height: 13.5,
            width: 76.5,
            title: "产品名称及规格",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 520.5,
            top: 174,
            height: 15,
            width: 30,
            title: "备注",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.hline",
          options: { left: 16.5, top: 208.5, height: 9, width: 558 },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 201,
            top: 225,
            height: 15,
            width: 120,
            title: "DS-M10/72H(单玻)-550",
            field: "ProductName",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 379.5,
            top: 225,
            height: 13.5,
            width: 30,
            title: "15",
            field: "PackCount",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 93,
            top: 225,
            height: 15,
            width: 54,
            title: "000152",
            field: "Code",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 442.5,
            top: 225,
            height: 13.5,
            width: 28.5,
            title: "块",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 33,
            top: 225,
            height: 15,
            width: 18,
            title: "1",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 520.5,
            top: 225,
            height: 15,
            width: 30,
            title: " ",
            field: "Remark",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.hline",
          options: { left: 16.5, top: 259.5, height: 9, width: 558 },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 294,
            top: 334.5,
            height: 13.5,
            width: 84,
            title: "入库检验确认人：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 150,
            top: 334.5,
            height: 13.5,
            width: 45,
            title: "入库仓：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 22.5,
            top: 334.5,
            height: 13.5,
            width: 52.5,
            title: "出库仓：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 466.5,
            top: 334.5,
            height: 13.5,
            width: 33,
            title: "主管：",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.hline",
          options: { left: 16.5, top: 354, height: 9, width: 559.5 },
        },
        {
          tid: "configModule.universityDate",
          options: {
            left: 84,
            top: 364.5,
            height: 15,
            width: 120,
            dataType: "datetime",
            format: "yyyy-MM-dd HH:mm",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 471,
            top: 364.5,
            height: 15,
            width: 37.5,
            title: "第1页",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 525,
            top: 364.5,
            height: 13.5,
            width: 51,
            title: "共1页",
            fontSize: 10.5,
          },
        },
        {
          tid: "configModule.customText",
          options: {
            left: 22.5,
            top: 364.5,
            height: 15,
            width: 52.5,
            title: "印制日期：",
            fontSize: 10.5,
          },
        },
      ],
      paperNumberLeft: 565.5,
      paperNumberTop: 397.5,
      paperNumberDisabled: true,
    },
  ],
};

map.centerAndZoom(new BMap.Point(111.23141, 35.362931), 10);
map.setMapStyleV2({
  styleId: "a1328728e957eb51cf3f2c5feedb47b7",
});

var blist = [];
var districtLoading = 0;

function getBoundary() {
  addDistrict("盐湖区");
  addDistrict("永济市");
  addDistrict("河津市");
  addDistrict("绛县");
  addDistrict("夏县");
  addDistrict("新绛县");
  addDistrict("稷山县");
  addDistrict("芮城县");
  addDistrict("临猗县");
  addDistrict("万荣县");
  addDistrict("闻喜县");
  addDistrict("垣曲县");
  addDistrict("平陆县");
}

/**
 * 添加行政区划
 * @param {} districtName 行政区划名
 * @returns  无返回值
 */
function addDistrict(districtName) {
  //使用计数器来控制加载过程
  districtLoading++;
  var bdary = new BMap.Boundary();
  bdary.get(districtName, function (rs) {
    //获取行政区域
    var count = rs.boundaries.length; //行政区域的点有多少个
    if (count === 0) {
      alert("未能获取当前输入行政区域");
      return;
    }
    for (var i = 0; i < count; i++) {
      blist.push({ points: rs.boundaries[i], name: districtName });
    }
    //加载完成区域点后计数器-1
    districtLoading--;
    if (districtLoading == 0) {
      //全加载完成后画端点
      drawBoundary();
    }
  });
}

/**
 * 各种鼠标事件绑定
 */
function click(evt) {
  alert(evt.target.name);
}

function mouseover(evt) {
  evt.target.label.setZIndex(99);
  evt.target.label.setPosition(evt.point);
  evt.target.label.show();
}

function mousemove(evt) {
  evt.target.label.setPosition(evt.point);
}

function mouseout(evt) {
  evt.target.label.hide();
}

function drawBoundary() {
  //包含所有区域的点数组
  var pointArray = [];

  /*画遮蔽层的相关方法
   *思路: 首先在中国地图最外画一圈，圈住理论上所有的中国领土，然后再将每个闭合区域合并进来，并全部连到西北角。
   *      这样就做出了一个经过多次西北角的闭合多边形*/
  //定义中国东南西北端点，作为第一层
  var pNW = { lat: 59.0, lng: 73.0 };
  var pNE = { lat: 59.0, lng: 136.0 };
  var pSE = { lat: 3.0, lng: 136.0 };
  var pSW = { lat: 3.0, lng: 73.0 };
  //向数组中添加一次闭合多边形，并将西北角再加一次作为之后画闭合区域的起点
  var pArray = [];
  pArray.push(pNW);
  pArray.push(pSW);
  pArray.push(pSE);
  pArray.push(pNE);
  pArray.push(pNW);
  //循环添加各闭合区域
  for (var i = 0; i < blist.length; i++) {
    //添加显示用标签层
    var label = new BMap.Label(blist[i].name, {
      offset: new BMap.Size(20, -10),
    });
    label.hide();
    map.addOverlay(label);

    //添加多边形层并显示
    var ply = new BMap.Polygon(blist[i].points, {
      strokeWeight: 2,
      strokeColor: "#00FF00",
      fillOpacity: 1,
      fillColor: "#eeeeee",
    }); //建立多边形覆盖物
    ply.name = blist[i].name;
    //ply.name = blist[i].name;
    ply.label = label;
    ply.addEventListener("click", click);
    ply.addEventListener("mouseover", mouseover);
    ply.addEventListener("mouseout", mouseout);
    ply.addEventListener("mousemove", mousemove);
    map.addOverlay(ply);

    //添加名称标签层
    var centerlabel = new BMap.Label(ply.name, { offset: new BMap.Size(0, 0) });
    centerlabel.setPosition(ply.getBounds().getCenter());
    centerlabel.setStyle({
      color: "red",
      fontFamily: "微软雅黑",
    });
    //map.addOverlay(centerlabel);

    //将点增加到视野范围内
    var path = ply.getPath();
    pointArray = pointArray.concat(path);
    //将闭合区域加到遮蔽层上，每次添加完后要再加一次西北角作为下次添加的起点和最后一次的终点
    pArray = pArray.concat(path);
    pArray.push(pArray[0]);
  }

  //限定显示区域，需要引用api库
  var boundply = new BMap.Polygon(pointArray);
  //BMapLib.AreaRestriction.setBounds(map, boundply.getBounds());
  map.setViewport(pointArray); //调整视野

  //添加遮蔽层
  var plyall = new BMap.Polygon(pArray, {
    strokeOpacity: 0.0000001,
    strokeColor: "#000000",
    strokeWeight: 0.00001,
    fillColor: "#000000",
    fillOpacity: 0.4,
  }); //建立多边形覆盖物
  map.addOverlay(plyall);
}

setTimeout(function () {
  getBoundary();
}, 100);
